package com.uieffectforandroid.activityanim;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.os.Build.VERSION;
import android.util.Property;
import android.view.View;
import android.widget.FrameLayout;

public abstract class AnimatedDoorActivity extends Activity {

    private AnimatedDoorLayout mAnimated;
    protected int mDoorType;
    int sysVersion ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layoutResId());
        sysVersion= Integer.parseInt(VERSION.SDK);  
        if (sysVersion>11) {
        	ANIMATED_DOOR_LAYOUT_FLOAT_PROPERTY=new Property<AnimatedDoorLayout, Float>(Float.class, "ANIMATED_DOOR_LAYOUT_FLOAT_PROPERTY") {

                @Override
                public void set(AnimatedDoorLayout layout, Float value) {
                    layout.setProgress(value);
                }

                @Override
                public Float get(AnimatedDoorLayout layout) {
                    return layout.getProgress();
                }
            };
        	FrameLayout activityRoot = (FrameLayout) findViewById(android.R.id.content);
        	View parent = activityRoot.getChildAt(0);
        	
        	// better way ?
        	mAnimated = new AnimatedDoorLayout(this);
        	activityRoot.removeView(parent);
        	activityRoot.addView(mAnimated, parent.getLayoutParams());
        	mAnimated.addView(parent);
        	
        	mDoorType = getIntent().getIntExtra("door_type", AnimatedDoorLayout.HORIZONTAL_DOOR);
        	mAnimated.setDoorType(mDoorType);
        	
        	ObjectAnimator animator = ObjectAnimator.ofFloat(mAnimated, ANIMATED_DOOR_LAYOUT_FLOAT_PROPERTY, 1).setDuration(600);
        	animator.start();
        }
    }

    protected abstract int layoutResId();

    @Override
    public void onBackPressed() {
    	if (sysVersion>11) {
			
    		ObjectAnimator animator = ObjectAnimator.ofFloat(mAnimated, ANIMATED_DOOR_LAYOUT_FLOAT_PROPERTY, 0).setDuration(600);
    		animator.addListener(new AnimatorListenerAdapter() {
    			@Override
    			public void onAnimationEnd(Animator animation) {
    				finish();
    			}
    		});
    		animator.start();
		}else {
			finish();
		}
    }


    private static Property<AnimatedDoorLayout, Float> ANIMATED_DOOR_LAYOUT_FLOAT_PROPERTY ;

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, 0);
    }
}
